﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using System.IO;

namespace HCFramework.Product.FrontEndControl
{
    public partial class FilterProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadFilters();
        }

        private void LoadFilters()
        {
            if (!M.UrlQuery.categoryID.HasValue)
            {
                this.Visible = false;
                return;
            }
            ProductCategory category = C.ProductCategoryController.GetByID(M.UrlQuery.categoryID.Value);
            List<ProductProperty> listProperty = category.ProductProperties.Where(ik => ik.IsActive).OrderBy(ik => ik.OrderDisplay).ToList();
            foreach (ProductProperty property in listProperty)
            {
                string filterGroup = File.ReadAllText(Server.MapPath(@"~\Product\FrontEndControl\Template\FilterGroup.htm"));
                filterGroup = filterGroup.Replace("{propertyName}", property.Name);
                filterGroup = filterGroup.Replace("{filterItems}", GetDisplayItem(property));
                litFilter.Text += filterGroup;
            }
        }

        private string GetDisplayItem(ProductProperty property)
        {
            string ret = "";
            foreach (PropertyFilter filter in property.PropertyFilters.OrderBy(ik=>ik.OrderDisplay).ToList())
            {
                string item = File.ReadAllText(Server.MapPath(@"~\Product\FrontEndControl\Template\FilterItem.htm"));
                item = item.Replace("{filterValue}", filter.PropertyFilterID+"");
                item = item.Replace("{filterName}", filter.Value);
                item = item.Replace("{checked}", M.UrlQuery.filters.Contains(filter.PropertyFilterID) ? "checked='checked'": "");
                ret += item;
            }
            return ret;
        }
    }
}